package map;


import a_star.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;

	public class DrawMap extends View {

		private Bitmap image;
		int width;
		int height;
		float w;
		float h;
		float magnification;
		
		int split;
		float gapWidth;
		float gapHeight;
		Map map;
		
		Rect src= new Rect(0,0,width,height);
		RectF dst = new RectF(0, 0, w, h);
		Paint DivLine = new Paint();
		
		float x, y;
		double b1, b2, b3;
		public DrawMap(Context context, float x, float y, double b1, double b2, double b3) {
			super(context);
			setFocusable(true);
			image=BitmapFactory.decodeResource(context.getResources(),com.example.nevigation.R.drawable.map);
			
			this.x = x;
			this.y = y;
			this.b1 =b1; this.b2 = b2; this.b3 = b3; 
			
			// image info set
			width=image.getWidth();
			height=image.getHeight();
			
			// magnification is image downsize rate
			magnification = 0.7f;
			w=(float) (width*magnification);
			h=(float) (height*magnification);
			
			// how many image split slice? -> split var
			split = 50;
			
			//split gap calculate
			gapWidth = w/split;
			gapHeight = h/split;
		
			// create Map mode 1 for bind
			map = new Map(split, split, 1);
			map.createNode(w, h, split);
			map.setStartPoint(391, 281);
			map.setGoalPoint(659,45);
		}

		@Override
		protected void onDraw(Canvas canvas){
			// Show map.jpg
			Rect src= new Rect(0,0,width,height);
			RectF dst = new RectF(0, 0, w, h);
			Paint DivLine = new Paint();
			
			DivLine.setColor(Color.RED);
			canvas.drawBitmap(image, src,dst,null);
	
			// map function execute
			map.setCanvas(canvas);
			//map.findBestPath();
			map.draw();
			
			map.drawBeacon(48, 39, b1);
			map.drawBeacon(48, 15, b2);
			map.drawBeacon(35, 25, b3);
			
			map.drawPoint(x, y);
			
			DivLine.setColor(Color.BLUE);
			for(int i = 0 ;i<split;i++){
				canvas.drawLine(0,i*gapHeight,w,i*gapHeight,DivLine);
				canvas.drawLine(i*gapWidth,0,i*gapWidth,h,DivLine);		 
			}
			
			super.onDraw(canvas);
		}
		
	}